<?php
/**
 * @filesource modules/event/controllers/IndexController.php
 * @author Hoang
 * @abstract : List of actions in order
 *   - new
 *   - view
 *   - edit
 *   - delete
 *   - search
 */ 
class Home_MasterController extends Clu_Controller_Action_Master
{
	public $m,$c;//, $mPlace, $mArticle;
	public $tl;
	public function init()
	{
		include (APP_PATH.'/modules/place/Model/Place.php');
		include (APP_PATH.'/modules/event/Model/Event.php');
		include (APP_PATH.'/modules/category/Model/Category.php');
		//require_once (APP_PATH.'/modules/tour/Model/Tour.php');
//		//require_once (APP_PATH.'/modules/user/Model/User.php');
//		//require_once (APP_PATH.'/modules/tour/Model/TLeg.php');
//		$this->mArticle = new article_Model_Service();
//		$this->m = new Model_Tour();
		//$this->c = new category_Model_Category();
//		$this->tl = new Model_TLeg();
		$this->mPlace = new place_Model_Place();
		$this->mEvent = new event_Model_Service();
		$this->mTour  = new Model_Tour();
 		
		parent::init();
		
	}
	
	public function indexAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		$simpleSearchRequest = array(
                                'module' => 'home',
                                'controller' => 'block',
                                'action' => 'simpleSearch',
                                'regionId' => 4,
                                'boxWeight' => 1,
								'page' => 'home'
                );
		$this->addMiniAction($simpleSearchRequest);
				
		$eventRequest = array(
                                'module' => 'event',
                                'controller' => 'block',
                                'action' => 'featuredList',
                                'regionId' => 4,
                                'boxWeight' => 3,
								'page' => 'home'
                );
		$this->addMiniAction($eventRequest);
		
		
		$placeRequest = array(
                                'module' => 'place',
                                'controller' => 'block',
                                'action' => 'featuredList',
                                'regionId' => 4,
                                'boxWeight' => 2,
								'page' => 'home'
                );
		$this->addMiniAction($placeRequest);
		
		$tourRequest = array(
                                'module' => 'tour',
                                'controller' => 'block',
                                'action' => 'featuredList',
                                'regionId' => 4,
                                'boxWeight' => 4,
								'page' => 'home'
                );
		$this->addMiniAction($tourRequest);
         

        //todo: just sample to test layout. to be removed
		$placeBrowseByCategoriesRequest = array(
				'module' => 'place',
				'controller' => 'block',
				'action' => 'browseByCategory',
				'regionId' => 5,
				'boxWeight' => 0,
		);
		$this->addMiniAction($placeBrowseByCategoriesRequest);
		
		/*
		$infoSupportRequest = array(
				'module' => 'common',
				'controller' => 'block',
				'action' => 'infoSupport',
				'regionId' => 5,
				'boxWeight' => 3,
		);
		$this->addMiniAction($infoSupportRequest);
		*/
		//map
		/*
		$map = $this->view->render($sess->viewLang . '/index/mapvietnam.phtml');
		$this->setHolder($map, 5,1);
		$place = "To be done ";
		$this->setHolder($place, 4,1);
		*/
		//cities list + paginator
		$data['getArticle'] = 1;
		/*
		$paginator = $this->getListCities($data);
       	$this->view->paginator = $paginator;
		$this->view->listCities = $paginator->getCurrentItems();
		$this->renderToHolder('/hvn/listCities.phtml', false,4,2);
		*/
//		die_a($this->view->listCities);
		//event list + pagination
		//$now = getdate(time());
  		//$time = mktime(0,0,0, $now['mon'], 1, $now['year']);
  		//$date = getdate($time);
  		//$start_date= date("d/m/Y");
  		//$finish_date = date("d/m/Y", $date['0'] + 7776000);
		//$data1 = array('frequency' => 'monthly', 'startDate' => $start_date, 'finishDate' => $finish_date);
		//$paginatorEvent = $this->getListEvent($data1);
//		die_a($paginatorEvent);
       	//$this->view->paginator = $paginatorEvent;
//		die_a($paginatorEvent->getCurrentItems());
		//$this->view->listEvents = $paginatorEvent->getCurrentItems();
		//$this->renderToHolder('/hvn/listEvent.phtml', false,4,3);
		
		//tour list + paginator
		//$paginatorTour = $this->getListTour($data);
       	//$this->view->paginatorTour = $paginatorTour;
		//$this->view->listTour = $paginatorTour->getCurrentItems();
		//$this->renderToHolder('/hvn/listTour.phtml', false,4,4);	
		//die('shit');
//		die_a($this->view);
	}
	
	public function getListCities($data)
	{
		$select = $this->mPlace->search($data);
		$paginator = $this->generatePaginator($select, 1, 10, 5, 'Jumping');
				
      	//cache in $_SESSION
       	$sess = new Zend_Session_Namespace("paginator");
       	$sess->place_admin = array("search_params" => $data);
        $data = $sess->place_admin;
        if(isset($data))
        {
    		$select = $this->mPlace->search($data['search_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), 10,5, 'Jumping');
        }
        return $paginator;
	}
	
	public function getListEvent($data)
	{
	   	$select = $this->mEvent->search($data);
	   	$paginator = $this->generatePaginator($select, 1, 10, 5, 'Jumping');
	   	$sess = new Zend_Session_Namespace("paginatorEvent");
	   	$sess->event_admin = array("event_params" => $data);
        $data = $sess->event_admin;
				
      	//cache in $_SESSION
        if(isset($data))
        {
   			$select = $this->mEvent->search($data['event_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), 10,5, 'Jumping');
        }
        return $paginator;
	}
	
	public function getListTour($data)
	{
		$select = $this->mTour->search($data);
	   	$paginator = $this->generatePaginator($select, 1, 10, 5, 'Jumping');
	   	$sess = new Zend_Session_Namespace("paginatorTour");
	   	$sess->tour_admin = array("tour_params" => $data);
        $data = $sess->tour_admin;
				
      	//cache in $_SESSION
        if(isset($data))
        {
   			$select = $this->mTour->search($data['tour_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), 10,5, 'Jumping');
        }
        return $paginator;	
	}
	
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
}
?>
